/*
 *  acerwificontroller.c - Acer Laptop ACPI Wifi Controller Gnome Applet
 *
 *
 *  Copyright (C) 2007      Fabio Fucci <fabio.fucci@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *  To learn more see the project home page at:
 *  http://code.google.com/p/acerwificontroller/
 *
 *  Credits:
 *	Many thanks to the acer_acpi team. See http://code.google.com/p/aceracpi
 *
 */
#include <panel-applet.h>
#include <gtk/gtk.h>

static gboolean ENABLED = FALSE;

static void enable(GtkWidget *image) {
	system("gksu \"echo enabled : 1 > /proc/acpi/acer/wireless\"");
	ENABLED = TRUE;
	gtk_widget_set_sensitive(image, ENABLED);
	g_debug("Wireless enabled");
}

static void disable(GtkWidget *image) {
	system("gksu \"echo enabled : 0 > /proc/acpi/acer/wireless\"");
	ENABLED = FALSE;
	gtk_widget_set_sensitive(image, ENABLED);
	g_debug("Wireless disabled");
}

/*
 * The code below does not work due to a problem with gksu.
 */
/*
static gboolean enable_wifi(GtkWidget *widget, GdkEventButton *event, gpointer user_data) {
	system("gksu \"echo enabled : 1 > /proc/acpi/acer/wireless\"");
	
	return FALSE;
}

static gboolean disable_wifi(GtkWidget *widget, GdkEventButton *event, gpointer user_data) {
	system("gksu \"echo enabled : 0 > /proc/acpi/acer/wireless\"");
	
	return FALSE;
}

static void do_popup_menu (GtkWidget *my_widget, GdkEventButton *event) {
	GtkWidget *menu;
	GtkWidget *enable_menuitem;
	GtkWidget *disable_menuitem;
	int button, event_time;
	
	menu = gtk_menu_new();
	g_signal_connect (menu, "deactivate", 
                    G_CALLBACK (gtk_widget_destroy), NULL);
                    
	enable_menuitem = gtk_menu_item_new_with_label("Enable");
	disable_menuitem = gtk_menu_item_new_with_label("Disable");
	
	gtk_menu_append(GTK_MENU(menu), enable_menuitem);
	gtk_menu_append(GTK_MENU(menu), disable_menuitem);
	
	g_signal_connect (G_OBJECT(enable_menuitem), "button_press_event", 
                    G_CALLBACK (enable_wifi), (gpointer) menu);
	g_signal_connect (G_OBJECT(disable_menuitem), "button_press_event", 
                    G_CALLBACK (disable_wifi), (gpointer) menu);
	
	gtk_widget_show(enable_menuitem);
	gtk_widget_show(disable_menuitem);
	
	if (event) {
		button = event->button;
		event_time = event->time;
	} else {
		button = 0;
		event_time = gtk_get_current_event_time ();
	}
	
	gtk_menu_attach_to_widget (GTK_MENU (menu), my_widget, NULL);
	gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 
                  button, event_time);
}
*/

static gboolean on_button_press(GtkWidget *event_box, GdkEventButton *event, gpointer data) {
	/* Ignores buttons other than the left mouse button;
	   return FALSE so the event is passed to the default handler */
	if (event->button != 1) {
		return FALSE;
	} else {
		if (event->type == GDK_BUTTON_PRESS) {
			// do_popup_menu (event_box, event);
			if (ENABLED) {
				disable(GTK_WIDGET(data));
			} else {
				enable(GTK_WIDGET(data));
			}
		}
		
		return TRUE;
	}
}

/*
 * This is the applet's main method.
 */
static gboolean acerwificontroller_applet_fill(PanelApplet *applet, const gchar *iid, gpointer data) {
	if (strcmp (iid, "OAFIID:AcerWifiController") != 0) {
		return FALSE;
	}
	
	GtkWidget *image;
	GtkWidget *event_box;
	
	image = gtk_image_new_from_file("/usr/share/icons/gnome/22x22/devices/network-wireless.png");
	
	event_box = gtk_event_box_new();
	
	enable(image);
	
	gtk_container_add (GTK_CONTAINER (event_box), image);
	g_signal_connect (G_OBJECT (event_box), 
                  "button_press_event",
                  G_CALLBACK (on_button_press),
                  (gpointer) image);
	
	gtk_container_add(GTK_CONTAINER(applet), event_box);
	
	gtk_widget_show_all(GTK_WIDGET(applet));
	
	return TRUE;
}

PANEL_APPLET_BONOBO_FACTORY ("OAFIID:AcerWifiController_Factory",
                             PANEL_TYPE_APPLET,
                             "Acer Wifi Controller Applet",
                             "0.1",
                             acerwificontroller_applet_fill,
                             NULL);
